package com.myKng.auth.config;

import com.myKng.auth.model.Role;
import com.myKng.auth.repository.RoleRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public void run(String... args) throws Exception {
        // 初始化角色
        initRoles();
    }

    private void initRoles() {
        if (roleRepository.count() == 0) {
            log.info("初始化角色数据");
            
            List<Role> roles = Arrays.asList(
                Role.builder().name("USER").description("普通用户").build(),
                Role.builder().name("ADMIN").description("管理员").build(),
                Role.builder().name("MODERATOR").description("内容审核员").build()
            );
            
            roleRepository.saveAll(roles);
            log.info("角色数据初始化完成");
        }
    }
}